package enumdemo;

public enum TestEnum {
    // 枚举对象
    RED("红色", 0),
    BLACK("黑色", 1),
    GREEN("绿色", 2),
    WHITE("白色", 3);

    public String color;
    public int ordinal;

    // 枚举的构造方法默认是私有的
    private TestEnum() {

    }

    private TestEnum(String color, int ordinal) {
        this.color = color;
        this.ordinal = ordinal;
    }

    public static void main(String[] args) {
        TestEnum test = TestEnum.valueOf("GREEN"); // 将普通字符串转换为枚举实例
        System.out.println(test);

        System.out.println(RED.compareTo(GREEN)); // 比较两个枚举成员在定义时的顺序
    }

    public static void main2(String[] args) {
        TestEnum[] testEnum = TestEnum.values();
        for (int i = 0; i < testEnum.length; i++) {
            System.out.println(testEnum[i] + "->" + testEnum[i].ordinal());
        }
    }

    public static void main1(String[] args) {
        TestEnum testEnum2 = TestEnum.BLACK;
        switch (testEnum2) {
            case RED:
                System.out.println("red");
                break;
            case BLACK:
                System.out.println("black");
                break;
            case WHITE:
                System.out.println("WHITE");
                break;
            case GREEN:
                System.out.println("black");
                break;
            default:
                break;
        }
    }
}
